import { observable, makeObservable, action, runInAction } from 'mobx';
import { findCities } from '@/api/MaiZuo';

class CityStore {
  id = 110100;
  name = '北京';
  list = [];

  constructor() {
    makeObservable(this, {
      id: observable,
      name: observable,
      list: observable,
      change: action,
      changeList: action,
      fetchList: action,
    });
  }

  change(city = {}) {
    const { id, name } = city;
    this.id = id;
    this.name = name;
  }

  changeList(list = []) {
    this.list = list;
  }

  fetchList() {
    // 异步
    findCities().then((res) => {
      runInAction(() => {
        this.list = res.cities || [];
      });
    });
  }
}

export default CityStore;
